﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using FilStartUps.FILAPI;
using System.Web.Script.Serialization;
using System.Net;

namespace FilStartUps.FILAPI
{
    public class FilStartUpsAPI
    {
        //TEMP URL
        private const string BaseUrl = "http://192.168.0.107:8099/FilstartUpsRest/filstartupsservice/rest/";

        //convert url string
        private static string GetJson(string url)
        {
            WebClient wc = new WebClient();
            string json = wc.DownloadString(url);
            wc.Dispose();
            return json;
        }

        public static bool ForumViews(int forumid)
        {
            string fullUrl = BaseUrl + "forum/ForumViews/" + forumid;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            return true;

        }
        //Parse Event Post
        public static IEnumerable<EventPost> GetLatestEvent()
        {
            string fullUrl = BaseUrl + "post/GetLatestEvents";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var post = JsonConvert.DeserializeObject<IEnumerable<EventPost>>(stuff["data"].ToString());
            return post;
        }

        //GetImage
        public static PostImage GetPostImage(int postid)
        {

            string fullUrl = BaseUrl + "post/GetPostImage/" + postid;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var post = JsonConvert.DeserializeObject<PostImage>(stuff["data"].ToString());
            return post;
        }

        //GetFeaturedPost
        public static List<Post> GetFeaturedPost()
        {
            string fullUrl = BaseUrl + "post/GetFeaturedPost/";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var post = JsonConvert.DeserializeObject<List<Post>>(stuff["featured"].ToString());
            return post;
        }

        //FRONTEND PARSE DATA- POSTARTICLE DETAILS
        public static Post GetPostDetails(int id)
        {
            string fullUrl = BaseUrl + "post/GetPostDetails/" + id;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var post = JsonConvert.DeserializeObject<Post>(stuff["data"].ToString());
            return post;
        }

        //GET PAGESUB/CATEGORY LATEST ARTICLE
        public static CategoryPost GetCategoryPost(int id)
        {
            string fullUrl = BaseUrl + "category/GetCategoryPost/" + id;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var subpage_post = JsonConvert.DeserializeObject<CategoryPost>(stuff["catpost"].ToString());
            return subpage_post;
        }

        //Category Page
        //return List = catidPK, categoryname
        public static List<Category> GetPageSub(int id)
        {
            string fullUrl = BaseUrl + "category/GetPageSub/" + id;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var subpage = JsonConvert.DeserializeObject<List<Category>>(stuff["pagesub"].ToString());
            return subpage;
        }

        //ForumMainTopic
        //return list = forumidPK, forumname, forumdescription
        public static List<Forum> GetForumTopic()
        {
            string fullUrl = BaseUrl + "forum/GetForumTopic ";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var forum = JsonConvert.DeserializeObject<List<Forum>>(stuff["forum"].ToString());
            return forum;
        }

        //PostArchieves
        //return List = postidPK, posttitle, postdescription
        public static List<Post> GetArticleArchieves(int catidFK)
        {
            string fullUrl = BaseUrl + "post/GetArticleArchieves/" + catidFK;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var article = JsonConvert.DeserializeObject<List<Post>>(stuff["archieves"].ToString());
            return article;
        }

        //Get Article Details
        //return CategoryPost Class
        public static CategoryPost GetArticleDetails(int postidPK)
        {
            string fullUrl = BaseUrl + "post/GetPostDetails/" + postidPK;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var article = JsonConvert.DeserializeObject<CategoryPost>(stuff["data"].ToString());
            return article;
        }

        //List of Nav
        //return pageidPK, pagename
        public static List<PageNavBar> GetPageNav()
        {
            string fullUrl = BaseUrl + "page/getpageinfo";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var page = JsonConvert.DeserializeObject<List<PageNavBar>>(stuff["data"].ToString());
            return page;
        }

        //Removed Pagename
        //GetPageInformation 
        //return pagedescription,pagename, pageidPK
        public static Page GetPageInfo(int pageidPK)
        {
            string fullUrl = BaseUrl + "page/GetPageFullInfo/" + pageidPK;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var article = JsonConvert.DeserializeObject<Page>(stuff["pageinfo"].ToString());
            return article;
        }

        //Get Latest News
        //return postidPK, posttitle, postdescription
        public static List<Post> GetNewsPost()
        {
            string fullUrl = BaseUrl + "post/getnewsdetails";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var news = JsonConvert.DeserializeObject<List<Post>>(stuff["data"].ToString());
            return news;
        }

        //Get Latest Articles
        //return postidPK, posttitle, postdescription
        public static List<Post> GetLatestArticles()
        {
            string fullUrl = BaseUrl + "post/getlatestarticles";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var news = JsonConvert.DeserializeObject<List<Post>>(stuff["data"].ToString());
            return news;
        }

    }
}